/*
 * @Author: wangkg wangkegui@longfor.com
 * @Date: 2024-04-09 09:41:06
 * @Description:
 */
import type { GlobEnvConfig } from '#/config';
import pkg from '../../package.json';

export function getCommonStoragePrefix() {
  const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();
  return `${VITE_GLOB_APP_TITLE.replace(/\s/g, '_')}__${getEnv()}`.toUpperCase();
}

// Generate cache key according to version
export function getStorageShortName() {
  return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}

// const getVariableName = (title: string) => {
//   function strToHex(str: string) {
//     const result: string[] = [];
//     for (let i = 0; i < str.length; ++i) {
//       const hex = str.charCodeAt(i).toString(16);
//       result.push(('000' + hex).slice(-4));
//     }
//     return result.join('').toUpperCase();
//   }
//   return `__PRODUCTION__${strToHex(title) || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, '');
// };

export function getAppEnvConfig() {
  const ENV = (import.meta.env as unknown as GlobEnvConfig) || ({} as GlobEnvConfig);
  const { VITE_GLOB_APP_TITLE, VITE_GLOB_UPLOAD_URL, VITE_APP_BASE_API, VITE_APP_ID } = ENV;
  return {
    VITE_GLOB_APP_TITLE,
    VITE_APP_BASE_API,
    VITE_GLOB_UPLOAD_URL,
    VITE_APP_ID
  };
}

/**
 * @description: Development mode
 */
export const devMode = 'development';

/**
 * @description: Production mode
 */
export const prodMode = 'production';

/**
 * @description: Get environment variables
 * @returns:
 * @example:
 */
export function getEnv(): string {
  return import.meta.env.MODE;
}

/**
 * @description: Is it a development mode
 * @returns:
 * @example:
 */
export function isDevMode(): boolean {
  return import.meta.env.DEV;
}

/**
 * @description: Is it a production mode
 * @returns:
 * @example:
 */
export function isProdMode(): boolean {
  return import.meta.env.PROD;
}
